var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'soc_cust_master.html', controller: ListExpMstCtrl}).
      when('/delete/', {templateUrl: 'soc_cust_master.html', controller: ListExpMstCtrl}).      
      when('/updte/', {templateUrl: 'soc_cust_master.html', controller: ListExpMstCtrl}). 
      when('/cancel/', {templateUrl: 'soc_cust_master.html', controller: ListExpMstCtrl}). 
      otherwise({redirectTo: '/'});
}]);

app.directive('blur', function () {
    return function (scope, elem, attrs) {
        elem.bind('blur', function () {
            scope.$apply(attrs.blur);
        });
    };
});

function ListExpMstCtrl($scope, $http,$window,$location,$filter)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 25;
	$scope.Math = window.Math;
	$scope.ed = false;
	$scope.newsoc_custmast = {};
	$scope.saf_arr = [];
	$scope.n_arr = [];	
	
	$http.get('soc_cust_data.php?q=1').success(function(data) {		
		for(var i=0; i<data.length;i++){
			//$scope.saf_arr[i] = data[i].saf;
			$scope.n_arr[i] = data[i].n;
		}
    });	
	$http.get('soc_cust_data.php?q=2').success(function(data) {		
		for(var i=0; i<data.length;i++)
			$scope.saf_arr[i] = data[i].saf;
    });	
	
    $http.get('soc_cust_data.php?q=3').success(function(data) {
		$scope.soc_custmast = data;
    });

	$scope.valch = function(){
		$scope.newsoc_custmast.socId = document.getElementById('soc').value;			
		for(var i=0; i<$scope.n_arr.length;i++){		
			if($scope.newsoc_custmast.soc == $scope.n_arr[i]){
				$scope.s_name = "( "+$scope.n_arr[i]+" )";
			}
		}
	};
	
	$scope.e = function() {
	$(function () 
	{				
		$( "#soc" ).autocomplete({
			source: $scope.n_arr
		});			
	});		
	};
	
	//Function to Edit Record
    $scope.edit = function(id) {		
         for(i in $scope.soc_custmast) {		 
			if($scope.soc_custmast[i].id == id)
            {          				
				$scope.newsoc_custmast = angular.copy($scope.soc_custmast[i]);
				$scope.newsoc_custmast.actdt = $scope.soc_custmast[i].actdt;
				$scope.newsoc_custmast.termdt = $scope.soc_custmast[i].termdt;
            }
        }
		$scope.ed = true;
		for(var i=0; i<$scope.n_arr.length;i++)		
			if($scope.newsoc_custmast.soc == $scope.n_arr[i])
				$scope.s_name = "( "+$scope.n_arr[i]+" )";				
    };
    
	
    //Function to Delete Record
   $scope.dlt = function(x) {
      if (confirm('Do you really want to delete this Customer record?')) 
        {
            var sql = 'soc_cust_cud.php?'+"custid="+x.id+"&cud=d";							
            $http.post(sql).success(function(data)
            {                               
				alert("Record "+x.id+" deleted successfully!");							
            });
            $http.get('soc_cust_data.php').success(function(data) {			
				$scope.soc_custmast = data;
			});   
        }
      };
      
	
		//Function to Update Data 
		$scope.upd = function(x) 
			{   
				x.soc = document.getElementById('soc').value;
						
				var chk=false;
				for(var i=0;i<$scope.n_arr.length;i++)
					if(x.soc  == $scope.n_arr[i])
						chk=true;
						
				if(!x.soc || !x.room || !x.fname ||!x.phone || !x.saf)
					alert('Please enter all the fields marked with *');		
				else if(!chk)
				alert("Enter a valid Society name");
				else{					
					var sql = 'soc_cust_cud.php?'+"dataset="+JSON.stringify(x)+"&cud=u";						
					$http.post(sql).success(function(data)  
					{					
						alert("Record "+x.id+" updated successfully!");		
						//location.href=sql;
					});
					$scope.cancel();
					$http.get('soc_cust_data.php').success(function(data) {
						$scope.soc_custmast = data;
					});
				}
			};
      
	 //Function to Insert Data 
	   $scope.sv = function(x) 
	  	{			
			x.soc = document.getElementById('soc').value;					
			var chk=false;
			for(var i=0;i<$scope.n_arr.length;i++)
				if(x.soc  == $scope.n_arr[i])
					chk=true;
			var chk2=false;
				for(var i=0;i<$scope.saf_arr.length;i++)
					if(x.saf  == $scope.saf_arr[i])
						chk2=true;
				for(var i=0;i<$scope.n_arr.length;i++)
					if(x.soc  == $scope.n_arr[i])
						chk1=true;
			
			if(!x.soc || !x.room || !x.fname ||!x.phone || !x.saf)
				alert('Please enter all the fields marked with *');		
			else if(!chk || chk2)
			{
				if(!chk)
					alert("Enter a valid Society name");
				if(chk2)
					alert("This saf exists...");
			}
				
			
			else{				
				
				var sql = 'soc_cust_cud.php?'+"dataset="+JSON.stringify(x)+"&cud=c";					
				$http.post(sql).success(function(data)  
				{				
					//location.href=sql;
					alert("Record saved successfully!");					
				});	
				$scope.cancel();
				$http.get('soc_cust_data.php').success(function(data) {
					$scope.soc_custmast = data;
				});
			}
		};
	  
	  //Function to Clear the selected parameters
        $scope.cancel= function()
		{
			$scope.activePath = null;
			$scope.activePath = $location.path('soc_cust_master.php');			
		};
      
 }
     //Custom Filter Function to sort the record.
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
	
	 app.directive('capitalize', function() {
   return {
     require: 'ngModel',
     link: function(scope, element, attrs, modelCtrl) {
        var capitalize = function(inputValue) {
           if(inputValue == undefined) inputValue = '';
           var capitalized = inputValue.toUpperCase();
           if(capitalized !== inputValue) {
              modelCtrl.$setViewValue(capitalized);
              modelCtrl.$render();
            }         
            return capitalized;
         }
         modelCtrl.$parsers.push(capitalize);
         capitalize(scope[attrs.ngModel]);  // capitalize initial value
     }
   };
});       
        